package platno;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import vypocty.VypocetSinusu;
import riadenie.*;
import utilities.*;

public class Kresli {
	  public int zaciatok, koniec;	
	  public int x1, y1, i, j, k, kn, l = 0, faza = 0;
	  public Dimension rozmery;
	  public String nula = "0.0", popis, staryPopis;
	  public Platno pl = null;
	  public Graphics gr;
	  private Graphics2D ig, g;
	  public RiadiaciDialog rd = null;
	  public Parametre p = null;
	  public VypocetSinusu s = null;
	  private Properties pr = new Properties();
      private BufferedImage image;
	  
	  public void setPlRef(Platno pl){		  
		  this.pl = pl;
		  gr = pl.getGraphics();
          prepareAntialiasing(gr);		  		  
	  }
	  public void setPRef(Parametre p){		  
		  this.p = p;		  
	  }
      private void prepareAntialiasing(Graphics gr){
		  g = (Graphics2D) gr;
	      image = new BufferedImage(pr.getPlatnoSizeX(), pr.getPlatnoSizeY(), BufferedImage.TYPE_INT_RGB);   // Create an off-screen image
		  ig = image.createGraphics();                                                                       // Get its Graphics for drawing
		  ig.setStroke(new BasicStroke(1.4f));                                                               // 1,4-pixel lines
//		  ig.setStroke(new BasicStroke(1.0f));                                                               // 1,4-pixel lines
		  ig.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);           // Anti-alias!
  
      }
      private void cleanCanvas(){
          ig.setColor(new Color(255, 255, 255));         // Vybielenie platna pred kreslenim !!!
          ig.fillRect(0, 0, pr.getPlatnoSizeX(), pr.getPlatnoSizeY());    	  
      }
      private void putXYAxis(){
          ig.setColor(new Color(0, 0, 0));
          ig.drawLine(25, (rozmery.height / 2), (rozmery.width - 10), (rozmery.height / 2));     // x-ova os
          ig.drawLine(40, 7, 40, (rozmery.height - 10));                                         // y-ova os    	  
      }
      private void putYAxisMarks(){
    	  // Znacky na y-ovej osi
          // zovseobecnit !!!
          ig.drawLine(38, ((rozmery.height + 18) / 4), 42, ((rozmery.height + 18) / 4));
          ig.drawLine(38, 9, 42, 9);
          ig.drawLine(38, ((3 * rozmery.height - 18) / 4), 42, ((3 * rozmery.height - 18) / 4));
          ig.drawLine(38, (rozmery.height - 12), 42, (rozmery.height - 12));    	  
      }
      private void putYAxisDesc(){
    	// Popis y-ovej osi
	          
          ig.drawString(nula, 5,((rozmery.height / 2) + 4));    // Nula sa nepremazava - nemeni sa !

          ig.setColor(Color.black);
          popis = String.valueOf(Rounding.round(s.max,2));
          ig.drawString(popis, 15, 13);
          
          popis = String.valueOf(Rounding.round(s.max / 2,2));
          ig.drawString(popis, 13,(((rozmery.height + 18) / 4) + 4));
          	          
          popis = String.valueOf(Rounding.round(s.min,2));
          ig.drawString(popis, 10,((rozmery.height - 12) + 5));
          
          popis = String.valueOf(Rounding.round(s.min / 2,2));
          ig.drawString(popis, 10,(((3 * rozmery.height - 18) / 4) + 4));    	  
      }
      private void putXAxisMarks(){
   		// Znacky na x-ovej osi          
          
          ig.drawLine((90 + 40), (rozmery.height / 2 - 2), (90 + 40), (rozmery.height / 2 + 2));
          ig.drawLine((180 + 40), (rozmery.height / 2 - 2), (180 + 40), (rozmery.height / 2 + 2));
          ig.drawLine((270 + 40), (rozmery.height / 2 - 2), (270 + 40), (rozmery.height / 2 + 2)); 
          ig.drawLine((360 + 40), (rozmery.height / 2 - 2), (360 + 40), (rozmery.height / 2 + 2)); 
          ig.drawLine((450 + 40), (rozmery.height / 2 - 2), (450 + 40), (rozmery.height / 2 + 2));
          ig.drawLine((540 + 40), (rozmery.height / 2 - 2), (540 + 40), (rozmery.height / 2 + 2)); 
          ig.drawLine((630 + 40), (rozmery.height / 2 - 2), (630 + 40), (rozmery.height / 2 + 2));     	  
      }
      private void putGraph(){
         for(i = zaciatok; i < koniec; i++) { 
             s.ziskajSur(i, faza); 
             ig.setColor(Color.blue);
             x1 = 40 + s.x1;             
             y1 = (rozmery.height/ 2) - (int)(((rozmery.height / 2) - 9) * s.y1);

             ig.drawLine(x1, y1, x1, y1); 
         }    	  
      }
      
      public void maluj(){
	  
	      rozmery = pl.getSize();
	      zaciatok = 0;
	      koniec = rozmery.width - 49;

	      s = new VypocetSinusu(zaciatok, koniec, p);	      

	      cleanCanvas(); 
          putXYAxis();	          
          putYAxisMarks();
          putYAxisDesc();
	      putXAxisMarks();          
	      putGraph();    
	          
	      g.drawImage(image, null, 0, 0);
      }
}